<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String gd_id = request.getParameter("gd_id") == null ? "" : request.getParameter("gd_id");
	String gd_content_id = request.getParameter("gd_content_id") == null ? "" : request.getParameter("gd_content_id");
	String dept_id = request.getParameter("dept_id") == null ? "" : request.getParameter("dept_id");
	String dept_name = request.getParameter("dept_name") == null ? "" : request.getParameter("dept_name");
	String gd_code = request.getParameter("gd_code") == null ? "" : request.getParameter("gd_code");
	String gd_name = request.getParameter("gd_name") == null ? "" : request.getParameter("gd_name");
	String customer_id = request.getParameter("customer_id") == null ? "" : request.getParameter("customer_id");
	String customer = request.getParameter("customer") == null ? "" : request.getParameter("customer");
	String borrow_id = request.getParameter("borrow_id") == null ? "" : request.getParameter("borrow_id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(borrow_id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建客户库借料":"编辑客户库借料" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		$(function(){
			//$('#borrow_code').val(strTime());
			$("#mSearchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 domSearch();
		       	} 
		   	}); 
			$("#material_type_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=0',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					domSearch();
				}
			});
			$('#mtb').hide();
			if('<%=flag%>' == 1){
				$('#ff').form('load','<%=contextPath %>/springMVC/gd/getCustomerBorrowById?borrow_id=<%=borrow_id%>');
			}else{
			    jQuery("#operation_date").datetimebox('setValue',getTodaySecond());
			}
			
			$('#table').datagrid({
				<%=isAdd?"url:'',":"url:'"+contextPath+"/springMVC/gd/listGDCustomerBorrowDetail?gd_id="+gd_id+"&gd_content_id="+gd_content_id+"&borrow_id="+borrow_id+"',"%>
				rownumbers:true,
				fitColumns:true,
				singleSelect:true,
				frozenColumns:[[
					{field:'ck',checkbox:true},
				    {title:'仓库id',field:'warehouse_id',hidden:true},
				    {title:'产品id',field:'material_id',hidden:true}
				]],
				columns:[[
					{title:'产品编号',field:'code',width:100,align:'center',editor:{
						type:'combogrid',
						options:{
							url: '<%=contextPath %>/springMVC/warehouse/listPageCustomerMaterial?gd_id=<%=gd_id%>&gd_content_id=<%=gd_content_id%>&customer_id=<%=customer_id%>',
						    panelWidth:750,
						    panelHeight:355,
						    rownumbers:true,
						    fitColumns:true,
						    columns: [[  
						       {field:'material_id',title:'材料主键',hidden:true},  
						       {field:'warehouse_id',hidden:true},
						       {field:'customer',title:'客户',width:100,align:'center',formatter:function(){
						    	   return '<%=customer%>';
						       }},
						       {field:'material_type_name',title:'产品类别',width:100,align:'center'},
						       {field:'code',title:'产品编号',width:100,align:'center'},
						       {field:'name',title:'产品名称',width:100,align:'center'},
						       {field:'format_type',title:'规格/型号',width:100,align:'center'},
						       {field:'unit_name',title:'单位',width:50,align:'center'},
						       {field:'num',title:'预算数量',hidden:true,width:50,align:'center'},
						       {field:'control',hidden:true},
						       {field:'max_num',title:'管控最大值',hidden:true,width:60,align:'center',formatter:function(value,row,index){
						    	   var num = parseFloat(row.num);
						    	   var control = parseFloat(row.control)/100;
						    	   row.max_num = Math.round(num*(1+control)*Math.pow(10,2))/Math.pow(10,2);
						    	   return row.max_num;
						       }},
						       {field:'already_use',title:'已使用数',hidden:true,width:50,align:'center'},
						       {field:'db_num',title:'库存总数',width:80,align:'center'},
						       {field:'md_queue_num',title:'审批占用数',width:80,align:'center'}
						    ]],
						    onBeforeLoad:function(param){
								var mSearchValue = $('#mSearchValue').val();
								var material_type_id = $('#material_type_id').combotree('getValue');
								param.searchValue=mSearchValue;
								param.material_type_id=material_type_id;
							},
						    toolbar:'#mtb',
						    pageList : [10,50,100],  
							pagination:true,
							onClickRow:function(rowIndex,rowData){
								if(checkSameProduct3(rowData,$('#table').datagrid('getData').rows)){
									var pRow = $('#table').datagrid('getRows')[lastIndex];
									pRow.warehouse_id = rowData.warehouse_id;
									pRow.material_id = rowData.material_id;
									pRow.code = rowData.code;
									pRow.name = rowData.name;
									pRow.unit_name = rowData.unit_name;
									pRow.format_type = rowData.format_type;
									pRow.assume_num = rowData.num;
									pRow.control = rowData.control;
									pRow.max_num = rowData.max_num;
									pRow.already_use = rowData.already_use;
									pRow.cost = 0;
									pRow.total_num = rowData.db_num;
									pRow.queue_num = rowData.md_queue_num;
									$('#table').datagrid('getData').rows[lastIndex]=pRow;
									var data = $('#table').datagrid('getData');
									$('#table').datagrid('loadData',data);
									
									$('#table').datagrid('endEdit', lastIndex);
									$('#table').datagrid('beginEdit', lastIndex);
									//lastIndex =-1;
								}else{
									$.messager.alert("提示","选择的记录已经存在");
								}
							}
						}
					}},
					{title:'产品名称',field:'name',width:100,align:'center'},
					{title:'规格/型号',field:'format_type',width:100,align:'center'},
					{title:'单位',field:'unit_name',width:50,align:'center'},
					{title:'单价',field:'cost',hidden:true},
					<%if(!"1".equals(isShow)){ %>
						<% if(isAdd){%>
						{title:'预算数量',hidden:true,field:'assume_num',width:50,align:'center'},
						{title:'管控最大值',hidden:true,field:'max_num',width:50,align:'center'},
						{title:'已使用数',hidden:true,field:'already_use',width:50,align:'center'},
						{title:'库存总数',field:'total_num',width:50,align:'center'},
						{title:'审批占用数',field:'queue_num',width:50,align:'center'},
						<%}%>
						<%if(!isAdd){%>
						{title:'预算数量',hidden:true,field:'assume_num',width:50,align:'center'},
						{field:'control',hidden:true},
						{title:'管控最大值',hidden:true,field:'max_num',width:50,align:'center',formatter:function(value,row,index){
							if(row.assume_num == undefined){
								return "";
							}
					    	   var assume_num = parseFloat(row.assume_num);
					    	   var control = parseFloat(row.control)/100;
					    	   row.max_num = Math.round(assume_num*(1+control)*Math.pow(10,2))/Math.pow(10,2);
					    	   return row.max_num;
					     }},
						{title:'已使用数',hidden:true,field:'already_use',width:50,align:'center'},
						{title:'库存总数',field:'total_num',width:100,align:'center',formatter:function(value,row,index){
							if(value == undefined){
								$.ajax({
									type:'POST',
									url:'<%=contextPath %>/springMVC/gd/getCustomerMaterialTotalNum?customer_id=<%=customer_id%>&material_id='+row.material_id,
									async:false,
									success:function(rtJson){
										row.total_num = rtJson.total_num;
										row.queue_num = rtJson.queue_num;
									}
								});
								return row.total_num;
							}
							return row.total_num;
						}},
						{title:'审批占用数',field:'queue_num',width:50,align:'center'},
					<%}}%>
					{title:'申请数量',field:'num',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{hidden:true,title:'归还时间',field:'return_time',width:150,align:'center',editor:{
						type:'datebox'
					}},
					{hidden:true,title:'要求到货时间',field:'request_time',width:150,align:'center',editor:{
						type:'datebox'
					}},
					{hidden:true,title:'收货人',field:'receiver',width:150,align:'center',editor:{
						type:'text'
					}},
					{hidden:true,title:'联系电话',field:'phone',width:150,align:'center',editor:{
						type:'text'
					}},
					{title:'备注',field:'remark',width:100,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'num'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
						id:'add',
						text:'选择材料',
						disabled:false,
						iconCls:'icon-add',
						handler:function(){
							addRow();
						}
					},'-',{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
					}
					},'-',{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						var rows = $('#table').datagrid('getSelections');
						if (rows.length > 0){
							for(var i=0;i<rows.length;i++){
								var row = rows[i];
								var index = $('#table').datagrid('getRowIndex', row);
								$('#table').datagrid('deleteRow', index);
							}
						}
						var data = $('#table').datagrid('getData');
						$('#table').datagrid('loadData',data);
					}
				}
			   ]
			});
		});
		function domSearch()
		{
			var mSearchValue = $('#mSearchValue').val();
			var material_type_id = $('#material_type_id').combotree('getValue');
			$(combogridTarget).combogrid('grid').datagrid('load',{
				searchValue:mSearchValue,
				material_type_id:material_type_id
				});
		}
		function addRow(){
			$('#table').datagrid('endEdit', lastIndex);
			$('#table').datagrid('appendRow',{
				total_num:'0',
				queue_num:'0',
				num:'0'
			});
			var data = $('#table').datagrid('getData');
			$('#table').datagrid('loadData',data);
			lastIndex = $('#table').datagrid('getRows').length-1;
			$('#table').datagrid('selectRow', lastIndex);
			$('#table').datagrid('beginEdit', lastIndex);
		}
		
		function addGDMaterialRequest(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			if($('#ff').form('validate')){
				var products="";
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					if(products!=""){
						products+=";";
					}
					products+=jsonToStr(jsondata.rows[i]);
					var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
					if(jsondata.rows[i].name == null || jsondata.rows[i].name == ""){
						$.messager.alert('提示','第'+index+'行未选择产品！');
						return ;
					}else if(jsondata.rows[i].num == null || jsondata.rows[i].num <= 0){
						$.messager.alert('提示','第'+index+'行数量填写不正确！');
						return ;
					}
					else if(parseFloat(jsondata.rows[i].num) + parseFloat(jsondata.rows[i].queue_num)> parseFloat(jsondata.rows[i].total_num)){
						$.messager.alert('提示','第'+index+'行申请数量不能大于库存总数！');
						return ;
					}
					/**
					else if(parseFloat(jsondata.rows[i].num)+parseFloat(jsondata.rows[i].already_use) > parseFloat(jsondata.rows[i].max_num)){
						$.messager.alert('提示','第'+index+'行数量已经超出管控最大范围！');
						return ;
					}
					**/
				}
				if(!checkNullOREmpty(products,'产品为必填项')){return;}
				var operation_date = $("#operation_date").datetimebox('getValue');
				var remark = encodeURIComponent($('#remark').val());
				var borrow_code = $('#borrow_code').val();
				
				var param = "type=0&borrow_code="+borrow_code+"&customer_id=<%=customer_id%>&gd_id=<%=gd_id%>&dept_id=<%=dept_id%>&dept_name=<%=dept_name%>&gd_code=<%=gd_code%>&gd_name=<%=gd_name%>&person_id=<%=person_id%>&person=<%=person%>&operation_date="+operation_date+"&remark="+remark+"&products="+products;
				$(obj).hide();
				$.ajax({
					type:'POST',
					<%=isAdd?"url:'"+contextPath+"/springMVC/gd/addGDCustomerBorrow',":"url:'"+contextPath+"/springMVC/gd/updateGDCustomerBorrow?borrow_id="+borrow_id+"',"%>
					data:param,
					dataType:'json',
					success:function(rtJson){
						if(rtJson.rtState == '0'){
							eval('window.opener.<%=callback%>();');
							window.close();
						}
						else{
							$.messager.alert("提示",rtJson.rtMsrg);
						}
						$(obj).show();
					}
				});
			}
		}
		 function gdPrint(){
				openDialogResize("<%=contextPath%>/springViews/print/startCustomerBorrowPrint.jsp?borrow_id=<%=borrow_id%>",1000, 800);
			}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>工单编号：</td>
					<td class="td2" nowrap>
						<input  id="gd_code" name="gd_code" readOnly value="<%=gd_code%>">
					</td>
					<td align="right" class="td1" nowrap>工单名称：</td>
					<td class="td2" nowrap>
						<input  id="gd_name" name="gd_name" readOnly value="<%=gd_name%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>申请部门：</td>
					<td class="td2" nowrap>
						<input  id="dept_name" name="dept_name" readOnly value="<%=dept_name%>">
					</td>
					<td align="right" class="td1" nowrap>申请人：</td>
					<td class="td2" nowrap>
						<input  id="person" name="person" readOnly value="<%=person%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>借料单号：</td>
					<td class="td2" nowrap >
						<input  id="borrow_code" name="borrow_code" class="easyui-validatebox" data-options="required:true">
					</td>
					<td align="right" class="td1" nowrap>申请日期：</td>
					<td class="td2" nowrap >
						<input  id="operation_date" name="operation_date" class="easyui-datetimebox" data-options="required:true">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
			   <%if(!"1".equals(isShow)){ %>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addGDMaterialRequest(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
				<%} else{%>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print',plain:false" onclick="gdPrint();">打印预览</a>
						</div>
					</td>
				</tr>
				<%}%>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
			<div id="mtb" style="padding:3px;height:auto">
				<div align="right">
					分类：<input  id="material_type_id" name="material_type_id" class="easyui-combotree">
					查询条件：<input id="mSearchValue" name="mSearchValue" style="width:150px" title="材料编码或名称"/>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="domSearch();"></a>
				</div>
			</div>
		
	</body>
</html>